using TEngine;
using UnityEngine;
using UnityEngine.UI;

namespace GameLogic
{
    /// <summary>
    /// 引导遮罩
    /// </summary>
    public class DumpPopupGuideMask : UIWidget
    {
        /// <summary>
        /// 遮罩组件
        /// </summary>
        private MaskComponent _maskComponent;
        /// <summary>
        /// 目标UI元素
        /// </summary>
        private Rect[] _targetRects;

        private int _CurrentTargetIndex = 0;
        private bool _IsShowing = false;

        public override void RegisterEvent()
        {
            base.RegisterEvent();
            AddUIEvent((int)EventCbType.ToggleGuide, ToggleGuide);
        }
        public override void OnDestroy()
        {
            base.OnDestroy();
        }

        public override void OnCreate()
        {
            base.OnCreate();
            if (_maskComponent == null)
            {
                _maskComponent = gameObject.GetComponent<MaskComponent>();
                _maskComponent.OnStart();
            }
            _targetRects = new Rect[]
            {
                new Rect(0.5f, 0.77f, 0.95f, 0.08f),
                new Rect(0.5f, 0.535f, 0.95f, 0.14f),
                new Rect(0.5f, 0.38f, 0.95f, 0.145f)
            };
        }

        public void ShowNext()
        {
            if (_targetRects == null || _targetRects.Length == 0) return;

            var targetRect = _targetRects[_CurrentTargetIndex];
            ShowGuideForRect(targetRect);
            _CurrentTargetIndex = (_CurrentTargetIndex + 1) % _targetRects.Length;
        }

        /// <summary>
        /// 为指定目标显示引导
        /// </summary>
        /// <param name="targetRect">目标RectTransform</param>
        /// <param name="shape">挖洞形状</param>
        /// <param name="padding">额外边距</param>
        public void ShowGuideForTarget(RectTransform targetRect, MaskComponent.HoleShape shape, Vector2? padding = null)
        {
            if (_maskComponent == null || targetRect == null) return;
            // 设置挖洞区域
            _maskComponent.SetHoleFromRectTransform(targetRect, shape, padding);
            // 显示遮罩
            _maskComponent.SetMask(true);
            _IsShowing = true;
        }

        /// <summary>
        /// 显示指定区域
        /// </summary>
        /// <param name="rect">指定区域</param>
        public void ShowGuideForRect(Rect rect)
        {
            if (_maskComponent == null) return;
            // 获取屏幕坐标
            _maskComponent.SetHoleRectangle(rect);
            _maskComponent.SetMask(true);
            _IsShowing = true;
        }

        /// <summary>
        /// 隐藏引导
        /// </summary>
        public void HideGuide()
        {
            if (_maskComponent == null) return;
            _maskComponent.SetMask(false);
            _IsShowing = false;
        }

        /// <summary>
        /// 切换引导显示状态
        /// </summary>
        public void ToggleGuide()
        {
            if (_IsShowing)
            {
                HideGuide();
            }
            else
            {
                ShowNext();
            }
        }

        /// <summary>
        /// 手动设置圆形挖洞
        /// </summary>
        /// <param name="center">中心位置（0-1归一化坐标）</param>
        /// <param name="radius">半径（0-1归一化坐标）</param>
        public void SetCircleHole(Vector2 center, float radius)
        {
            if (_maskComponent == null) return;

            _maskComponent.SetHoleCircle(center, radius);
            if (!_IsShowing)
            {
                _maskComponent.SetMask(true);
                _IsShowing = true;
            }
        }

        /// <summary>
        /// 手动设置矩形挖洞
        /// </summary>
        /// <param name="center">中心位置（0-1归一化坐标）</param>
        /// <param name="size">大小（0-1归一化坐标）</param>
        public void SetRectangleHole(Vector2 center, Vector2 size)
        {
            if (_maskComponent == null) return;

            _maskComponent.SetHoleRectangle(center, size);
            if (!_IsShowing)
            {
                _maskComponent.SetMask(true);
                _IsShowing = true;
            }
        }

        /// <summary>
        /// 手动设置圆角矩形挖洞
        /// </summary>
        /// <param name="center">中心位置（0-1归一化坐标）</param>
        /// <param name="size">大小（0-1归一化坐标）</param>
        /// <param name="cornerRadius">圆角半径</param>
        public void SetRoundedRectangleHole(Vector2 center, Vector2 size, float cornerRadius)
        {
            if (_maskComponent == null) return;

            _maskComponent.SetHoleRoundedRectangle(center, size, cornerRadius);
            if (!_IsShowing)
            {
                _maskComponent.SetMask(true);
                _IsShowing = true;
            }
        }

    }
}